package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.produce.domain.req.MachiningReq;
import com.ruibang.glass.produce.entity.CbMeasure;
import com.ruibang.glass.produce.entity.CbProcessWaterFlowRate;
import com.ruibang.glass.produce.mapper.CbProcessWaterFlowRateMapper;
import com.ruibang.glass.produce.service.CbProcessWaterFlowRateService;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.ZoneOffset;
import java.util.List;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-01-23  11:27
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Service
public class CbProcessWaterFlowRateServiceImpl extends ServiceImpl<CbProcessWaterFlowRateMapper, CbProcessWaterFlowRate> implements CbProcessWaterFlowRateService {
    @Override
    public IPage<CbProcessWaterFlowRate> pageQuery(MachiningReq machiningReq) {
        long end = machiningReq.getEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long start = machiningReq.getStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        final IPage<CbProcessWaterFlowRate> page = new Page<>(machiningReq.getPageNo(), machiningReq.getPageSize());
        return baseMapper.pageQuery(page, start, end, machiningReq.getGlassId());
    }

    @Override
    public void export(MachiningReq machiningReq, HttpServletResponse response) {
        machiningReq.setPageNo(-1);
        machiningReq.setPageSize(-1);
        IPage<CbProcessWaterFlowRate> page = this.pageQuery(machiningReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<CbProcessWaterFlowRate> cbProcessWaterFlowRateList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), CbProcessWaterFlowRate.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("长边研磨工艺水流量数据").doWrite(cbProcessWaterFlowRateList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }
}
